/**
 * 
 */
package edu.byu.nlp.pipes;

import java.util.Arrays;

/**
 * @author robbie
 *
 */
public class BasicInstance<L, D> implements Instance<L, D> {

	public final L label;
	public final String source;
	public final D data;
	
	public BasicInstance(L label, String source, D data) {
		this.label = label;
		this.source = source;
		this.data = data;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public L getLabel() {
		return label;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String getSource() {
		return source;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public D getData() {
		return data;
	}

	public static <L, D> Instance<L, D> of(L label, String source, D data) {
		return new BasicInstance<L, D>(label, source, data);
	}
	
	public static <L, D1, D2> Instance<L, D2> copyOf(Instance<L, D1> instance, D2 data) {
		return of(instance.getLabel(), instance.getSource(), data);
	}
	
	@Override
	public int hashCode() {
		return Arrays.deepHashCode(new Object[] { data, label, source });
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		
		if (obj == null)
			return false;
		
		@SuppressWarnings("rawtypes")		// necessary cast
		BasicInstance other = (BasicInstance) obj;
		return Arrays.deepEquals(
				new Object[] { data, label, source },
				new Object[] { other.data, other.label, other.source });
	}

	@Override
	public String toString() {
		return "BasicInstance [label=" + label + ", source=" + source + ", data=" + data + "]";
	}

}
